package com.koron.tool.task;

import com.koron.bean.app.AppBean;
import com.koron.bean.util.UUIDUtils;
import com.koron.common.core.config.ThreadLocalContext;
import com.koron.common.core.util.message.MessageUtil;
import com.koron.tool.bean.ToolBean;
import com.koron.tool.bean.ToolWarningInfoBean;
import com.koron.tool.enums.WarningReasonEnum;
import com.koron.tool.mapper.ToolAccountsManagerMapper;
import com.koron.tool.mapper.ToolWarningInfoMapper;
import com.koron.tool.util.ToolConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.koron.ebs.mybatis.ADOConnection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;


/**
 * @Author: laiguangming
 * @Date: 2022/10/20 9:30
 */
@Component
@Slf4j
public class TimeTask {

    @Autowired
    private MessageUtil messageUtil;

//    @Scheduled(fixedRate = 5000)
    @Scheduled(cron = "30 0 6,8 * * ?")
    public void generateWarningInfo() {
        try {
            // 查询即将到达校验期的工器具
            List<ToolWarningInfoBean> inspectionWarningInfoList = ADOConnection.runTask(ToolConstant.DB_ENV_WATER_02, sessionFactory -> {
                return sessionFactory.getMapper(ToolWarningInfoMapper.class).getInspectionWarningInfo();
            }, List.class);
            inspectionWarningInfoList.forEach(bean -> {
                bean.setId(UUIDUtils.getUuid());
                bean.setReason(WarningReasonEnum.REACH_THE_INSPECTION_PERIOD.getReason());
                bean.setStatus(ToolConstant.WARNING_INFO_STATUS_DISPLAY);
            });

            // 查询即将到达报废期的工器具
            List<ToolWarningInfoBean> scrapWarningInfoList = ADOConnection.runTask(ToolConstant.DB_ENV_WATER_02, sessionFactory -> {
                return sessionFactory.getMapper(ToolWarningInfoMapper.class).getScrapWarningInfo();
            }, List.class);
            scrapWarningInfoList.forEach(bean -> {
                bean.setId(UUIDUtils.getUuid());
                bean.setReason(WarningReasonEnum.REACH_THE_SCRAP_PERIOD.getReason());
                bean.setStatus(ToolConstant.WARNING_INFO_STATUS_DISPLAY);
            });
            // 合并集合
            List<ToolWarningInfoBean> list = new ArrayList<>();
            list.addAll(inspectionWarningInfoList);
            list.addAll(scrapWarningInfoList);
            // 批量插入到数据库中
            if (CollectionUtils.isNotEmpty(list)) {
                ADOConnection.runTask("water_02", sessionFactory -> {
                    return sessionFactory.getMapper(ToolWarningInfoMapper.class).batchInsertWarningInfo(list);
                }, Integer.class);
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    /**
     * 每天早上9:00发送待检验和报废信息
     */
//    @Scheduled(fixedRate = 5000)
    @Scheduled(cron = "0 0 9 * * ?")
    public void sendInspectAndScrapMsg() {
        AppBean appBean = new AppBean();
        appBean.setMark(ToolConstant.DB_ENV_WATER_02);
        ThreadLocalContext.setApp(appBean);
        List<ToolBean> inspectionToolList = ADOConnection.runTask(ToolConstant.DB_ENV_WATER_02, sessionFactory -> {
            return sessionFactory.getMapper(ToolAccountsManagerMapper.class).getTodayInspectionTool();
        }, List.class);
        Set<String> inspectionPersonSet = new HashSet<>();
        inspectionToolList.stream()
                .filter(toolBean -> StringUtils.isNotEmpty(toolBean.getPersonInCharge()))
                .forEach(toolBean -> {
                    String personInCharge = toolBean.getPersonInCharge();
                    String[] split = personInCharge.split("[,，；;]");
                    for (String userId : split) {
                        if (!inspectionPersonSet.contains(userId)) {
                            inspectionPersonSet.add(userId);
                            messageUtil.sendMessageWithTemplate("inspection_tool_notice", null, userId);
                        }
                    }
        });

        List<ToolBean> scrapToolList = ADOConnection.runTask(ToolConstant.DB_ENV_WATER_02, sessionFactory -> {
            return sessionFactory.getMapper(ToolAccountsManagerMapper.class).getTodayScrapTool();
        }, List.class);
    Set<String> scrapPersonSet = new HashSet<>();
        scrapToolList.stream()
                .filter(toolBean -> StringUtils.isNotEmpty(toolBean.getPersonInCharge()))
                .forEach(toolBean -> {
                    String personInCharge = toolBean.getPersonInCharge();
                    String[] split = personInCharge.split("[,，；;]");
                    for(String userId :split){
                        if (!scrapPersonSet.contains(userId)) {
                            scrapPersonSet.add(userId);
                            messageUtil.sendMessageWithTemplate("scrap_tool_notice", null, userId);
                        }
                    }
                });
    }

}
